package com.lancoo.ccas53.util;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * @author quin
 * @ClassName TimeUtils.java
 * @Description 时间工具类
 * @createTime 2022年03月23日 19:36:00
 */
public class TimeUtils {


    /**
     * 获取今天是一段日期的第几周(总周数）
     *
     * @param beginDateStr
     * @param endDateStr
     * @return
     */
    public static Integer getWeek(String beginDateStr, String endDateStr, DateTime currentDate) {
        DateTime beginDate = DateUtil.parseDate(beginDateStr);
        DateTime endDate = DateUtil.parseDate(endDateStr);
//        DateTime currentDate = DateUtil.dateNew(new Date());
        int week = 1;
        if (DateUtil.isIn(currentDate, beginDate, endDate)) {
            Date endDateOfWeek = DateUtil.endOfWeek(beginDate);
            while (!DateUtil.isIn(currentDate, beginDate, endDateOfWeek)) {
                ++week;
                endDateOfWeek = DateUtil.endOfWeek(DateUtil.offsetDay(endDateOfWeek, 1));
            }
            return week;
        }
        return null;
    }

    /**
     * 获取一段时间有多少周
     *
     * @param start
     * @param end
     * @return
     */
    public static Long getTotalWeek(LocalDateTime start, LocalDateTime end) {
        LocalDateTime startMondayTime = start.with(DayOfWeek.MONDAY);
        LocalDateTime endSundayTime = end.with(DayOfWeek.SUNDAY);
        long weekNum = LocalDateTimeUtil.between(startMondayTime, endSundayTime, ChronoUnit.WEEKS);
        return weekNum + 1;
    }

    /**
     * 获取一段时间 第几周 周几的日期
     *
     * @param beginDateStr
     * @param endDateStr
     * @param week
     * @param dayOfWeek
     * @return
     */
    public static DateTime getDateByWeek(String beginDateStr, String endDateStr, int week, int dayOfWeek) {
        DateTime dateTime = null;

        DateTime beginDate = DateUtil.parseDate(beginDateStr);
        DateTime endDate = DateUtil.parseDate(endDateStr);
        //第一周周一的日期
        DateTime monday = DateUtil.beginOfWeek(beginDate);

        //周数为0或1按照第一周计算
        if (week == 0 || week == 1) {
            dateTime = monday.offset(DateField.DAY_OF_MONTH, dayOfWeek - 1);
            //判断日期是否在学期内
            if (dateTime.isBefore(beginDate)) {
                return null;
            }
            return dateTime;
        }
        week--;
        dateTime = monday.offset(DateField.DAY_OF_MONTH, 7 * week + dayOfWeek - 1);
        if (dateTime.isAfter(endDate)) {
            return null;
        }
        return dateTime;
    }


    /**
     * 判断当前时间是否在上课时间内或者外 0：全部，1：已结束，2：进行中，3：未开始，4：进行中和未开始
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static Integer getStatus(LocalDateTime startTime, LocalDateTime endTime) {
        int status = 1;
        LocalDateTime currentTime = LocalDateTime.now();
        if (currentTime.isBefore(startTime)) {
            status = 3;
        } else if (currentTime.isAfter(endTime)) {
            status = 1;
        } else if (currentTime.isAfter(startTime) && currentTime.isBefore(endTime)) {
            status = 2;
        }
        return status;
    }

    //获取一学期有多少周

    /**
     * 获取年级
     *
     * @param grade 2022级
     * @param term  20221
     * @return
     */
    public static String getGlobalGrade(String grade, String term) {
        Integer gradeNum = Integer.valueOf(grade.substring(0, 4));
        Integer termNum = Integer.valueOf(term.substring(0, 4));
        String globalGrade;
        switch (termNum - gradeNum) {
            case 0:
                globalGrade = "U1";
                break;
            case 1:
                globalGrade = "U2";
                break;
            case 2:
                globalGrade = "U3";
                break;
            case 3:
                globalGrade = "U4";
                break;
            case 4:
                globalGrade = "U5";
                break;
            default:
                globalGrade = "";
        }
        return globalGrade;
    }
//
//    public static String getWeekStr(int dateNumber) {
//        String str;
//        str = getWeek(dateNumber);
//        switch (str) {
//            case "1":
//                str = "星期日";
//                break;
//            case "2":
//                str = "星期一";
//                break;
//            case "3":
//                str = "星期二";
//                break;
//            case "4":
//                str = "星期三";
//                break;
//            case "5":
//                str = "星期四";
//                break;
//            case "6":
//                str = "星期五";
//                break;
//            case "7":
//                str = "星期六";
//                break;
//        }
//        return str;
//    }

    public static void main(String[] args) {
        LocalDate date1 = LocalDate.of(2023, 3, 1);
        LocalDate date2 = LocalDate.of(2023, 3, 6);
        long weeksBetween = ChronoUnit.WEEKS.between(date1, date2) + 1;
        System.out.println("Weeks between " + date1 + " and " + date2 + ": " + weeksBetween);

    }
}

